package com.apexsoft.monitor.front.console.config.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.apexsoft.front.common.support.BaseFrontController;
import com.apexsoft.front.common.support.Result;
import com.apexsoft.front.common.utils.annotation.CrudMethod;
import com.apexsoft.monitor.front.console.aop.AopAnnotation;
import com.apexsoft.monitor.front.console.pageModel.ConfigModel;
import com.baomidou.mybatisplus.plugins.pagination.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import com.apexsoft.monitor.front.console.config.service.ConfigService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller("configController")
@RequestMapping("/console/config")
public class ConfigController extends BaseFrontController {
    @Autowired
    @Qualifier("configService")
    private ConfigService configService = null;
    /**
     * 主机监控
     */
    @RequestMapping(value = "/list")
    public String list(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("form") ConfigModel configModel) throws Exception {
        Pagination page =new Pagination(configModel.getPageNum(),configModel.getPageSize());
        JSONObject result = this.configService.listByPage(request,page,configModel);
        request.setAttribute("result", result);
        return "console/config/list";
    }

    @RequestMapping(value = "/loadConfigByPage")
    public String loadConfigByPage(HttpServletRequest request, HttpServletResponse response,@ModelAttribute("form") ConfigModel configModel) throws Exception {
        Pagination page =new Pagination(configModel.getPageNum(),configModel.getPageSize());
        JSONObject result = this.configService.listByPage(request,page,configModel);
        this.printJSON(response, result);
        return null;
    }

    @CrudMethod
    @AopAnnotation(description="添加阀值")
    @RequestMapping(value = "/add")
    public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {

        JSONObject result = configService.add(request);
        this.printJSON(response, result);
        return null;

    }

    @CrudMethod
    @AopAnnotation(description="修改阀值信息")
    @RequestMapping(value = "/update")
    public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {
        JSONObject result = configService.update(request);
        this.printJSON(response, result);
        return null;

    }

    @CrudMethod
    @AopAnnotation(description="根据id删除节点")
    @RequestMapping(value = "/delete")
    public String delete(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String id = request.getParameter("id");
        JSONObject result = configService.delete(id);
        this.printJSON(response, result);
        return null;

    }

    @RequestMapping(value = "/getAllConfigs")
    public String getAllTargets(HttpServletRequest request, HttpServletResponse response, @ModelAttribute("form") ConfigModel configModel) throws Exception {
        JSONArray configArr = new JSONArray();
        Pagination page =new Pagination(configModel.getPageNum(),configModel.getPageSize());
        JSONObject result = configService.listByPage(request,page,configModel);
        if (result != null && result.getIntValue(Result.CODE) > 0) {
            JSONObject nullObj = new JSONObject();
            configArr.add(nullObj);
            configArr.addAll(0,result.getJSONArray(Result.ROWS));
        }
        this.printJSON(response, configArr);
        return null;
    }

}
